package club.mzwh.common.util;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HttpGetUtil {

	// 发起http get类型请求获取返回结果
    public static String httpGet(String urlstr) throws IOException {
        URL url = new URL(urlstr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.connect();
        InputStream is = conn.getInputStream();

        byte[] buff = new byte[is.available()];
        is.read(buff);
        String ret = new String(buff, "utf-8");

        is.close();
        conn.disconnect();

        return ret;
    }
    
    public static String get1(String baseUrl,Map<String, String> parameters ,Map<String, String> headers ){
        //创建一个httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建一个返回值对象
        CloseableHttpResponse response = null;
        String entityString=null;
        try {
            URIBuilder uriBuilder = new URIBuilder(baseUrl);
            if(parameters!=null&&parameters.size()>0){
                for (Map.Entry<String, String> temp : parameters.entrySet() ) {
                    //循环map里面的每一对键值对，然后获取key和value即时想要的参数的 key和value
                    uriBuilder.addParameter(temp.getKey(),temp.getValue());
                }
            }
            /*URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
            uriBuilder.addParameter("query", "花千骨");*/
            HttpGet get = new HttpGet(uriBuilder.build());//生产一个httpget连接
            //添加head， 用setheader 如果已经存在就重置， addheader如果已经存在进行新增同名的header
            if(headers!=null&&headers.size()>0){
                for (Map.Entry<String, String> temp : headers.entrySet() ) {
                    //循环map里面的每一对键值对，然后获取key和value即时想要的参数的 key和value
                    get.setHeader(temp.getKey(),temp.getValue());
                }
            }
            //执行请求
            response = httpClient.execute(get);
            //取响应的结果
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println(statusCode);
            HttpEntity entity = response.getEntity();
            entityString = EntityUtils.toString(entity, "utf-8");
            System.out.println(entityString);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(response !=null){
                    response.close();
                }
                if (httpClient!=null){
                    httpClient.close();
                }
 
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return entityString;
 
    }

    /**
     * 下载头像图片
     * @author pl
     * @param portrait
     * @return
     */
    public static byte[] downloadPortrait(String portraitUrl) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            URL url = new URL(portraitUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            byte[] buff = new byte[1024];
            int cnt;
            while ((cnt = is.read(buff)) > 0) {
                baos.write(buff, 0, cnt);
            }

            is.close();
            conn.disconnect();

            baos.close();
            return baos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    
    // 写头像文件
    public static void savePortrait(String fileName, byte[] portraitData) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fileName);
            fos.write(portraitData);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    }
}
